package com.ansion.utils;

import android.app.Application;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Build;

import androidx.annotation.ArrayRes;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;
import androidx.annotation.DrawableRes;
import androidx.annotation.RequiresApi;
import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.content.ContextCompat;

import com.ansion.log.VLog;

import java.util.Arrays;
import java.util.Locale;

public class ResourceUtils {
    private static Application application;
    private static Context themeContext;
    private static Context appContext;
    private static final String TAG = "ResourceUtils";


    public static void initApplication(Application application) {
        ResourceUtils.application = application;
        themeContext = application;
        appContext = application;
    }

    public static void resetContext() {
        Resources res = getApplication().getResources();
        Configuration configuration = new Configuration(res.getConfiguration());
        int nightNode = AppCompatDelegate.getDefaultNightMode();
        if (nightNode == AppCompatDelegate.MODE_NIGHT_NO) {
            configuration.uiMode = Configuration.UI_MODE_NIGHT_NO | (res.getConfiguration().uiMode & ~Configuration.UI_MODE_NIGHT_MASK);
        } else if (nightNode == AppCompatDelegate.MODE_NIGHT_YES) {
            configuration.uiMode = Configuration.UI_MODE_NIGHT_YES | (res.getConfiguration().uiMode & ~Configuration.UI_MODE_NIGHT_MASK);
        } else {
            configuration.uiMode = res.getConfiguration().uiMode;
        }
        themeContext = getApplication().createConfigurationContext(configuration);
    }

    public static Application getApplication() {
        if (application == null) {
            throw new IllegalStateException("Common library ResourceUtils is used before initialize!");
        }
        return application;
    }

    public static Object getSystemService(String name) {
        return getContext().getSystemService(name);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static <T> T getSystemService(Class<T> clazz) {
        return getContext().getSystemService(clazz);
    }

    public static String getString(@StringRes int stringId, Object... formatArgs) {
        String resStr = "?";
        if (formatArgs != null && formatArgs.length > 0) {
            try {
                resStr = getContext().getString(stringId);
            } catch (Exception e) {
                VLog.e(e, TAG, "ResourceUtils.getString[1] with args error:" + Arrays.toString(formatArgs));
            }
            //支持多语言format
            return String.format(Locale.getDefault(), resStr, formatArgs);
        } else {
            try {
                return getContext().getString(stringId);
            } catch (Exception e2) {
                VLog.e(e2, TAG, "ResourceUtils.getString[2] second time error:");
                return "?";
            }
        }
    }

    public static String[] getStringArray(@ArrayRes int res) {
        return getContext().getResources().getStringArray(res);
    }

    @ColorInt
    public static int getColor(@ColorRes int colorResourceId) {
        if (application != null) {
            return ContextCompat.getColor(getThemeContext(), colorResourceId);
        }
        return 0;
    }

    public static Drawable getDrawable(@DrawableRes int drawableResourceId) {
        if (application != null) {
            return ContextCompat.getDrawable(getThemeContext(), drawableResourceId);
        }
        return null;
    }

    public static Drawable getDrawable(@DrawableRes int drawableResourceId, int size) {
        if (application != null) {
            Drawable drawable = ContextCompat.getDrawable(getThemeContext(), drawableResourceId);
            if (drawable != null) {
                drawable.setBounds(0, 0, size, size);
            }
            return drawable;
        }
        return null;
    }

    public static Drawable getDrawable(Context context, @DrawableRes int drawableResourceId) {
        if (context != null) {
            return ContextCompat.getDrawable(context, drawableResourceId);
        }
        return null;
    }

    public static Context getContext() {
        if (appContext == null) {
            throw new IllegalStateException("Common library ResourceUtils is used before initialize!");
        }
        return appContext;
    }

    public static Context getThemeContext() {
        if (themeContext == null) {
            throw new IllegalStateException("Common library ResourceUtils is used before initialize!");
        }
        return themeContext;
    }

    public static ContentResolver getContentResolver() {
        return getContext().getContentResolver();
    }

    public static SharedPreferences getSharedPreferences(String name) {
        try {
            SharedPreferences sharedPreferences = getContext().getSharedPreferences(name, Context.MODE_PRIVATE);
            return sharedPreferences;
        } catch (Throwable t) {
            if (VLog.OPEN_LOG) {
                VLog.d(TAG, "fatal error", t);
            }
            return getContext().getSharedPreferences(name, Context.MODE_PRIVATE);

        }
    }

//    public static SharedPref getSharedPref() {
//        return SharedPref.getInstance(getContext());
//    }
//
//    public static SharedPref getSharedPref(Context context) {
//        if (context == null) {
//            return getSharedPref();
//        } else {
//            return SharedPref.getInstance(context);
//        }
//    }

    private static String VERSION = null;

    public static String getVersion() {

        if (VERSION != null)
            return VERSION;
        try {
            VERSION = getContext().getPackageManager().getPackageInfo(
                    getContext().getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            VLog.e(TAG, e);
        }
        return VERSION;
    }


    public static String getPackageName() {
        return application.getPackageName();
    }

    public static Drawable updateSVGColor(int drawableResId, int colorResId) {
        Drawable drawable = ResourceUtils.getDrawable(drawableResId);
        if (drawable != null) {
            drawable.setTint(ResourceUtils.getColor(colorResId));
        }
        return drawable;
    }

    public static int getDrawableId(String imgName) {
        Resources resources = getContext().getResources();
        return resources.getIdentifier(imgName, "drawable", getContext().getPackageName());
    }

}
